package com.cs.sso.service.impl;

import com.cs.common.encryption.AESEncryptDecrypt;
import com.cs.common.encryption.DESEncryptDecrypt;
import com.cs.common.sequence.UUIDGenerator;
import com.cs.common.vo.ReturnVo;
import com.cs.sso.dbo.Account;
import com.cs.sso.dbo.ChangeLog;
import com.cs.sso.exception.LoginException;
import com.cs.sso.repository.AccountRepository;
import com.cs.sso.service.AccountService;
import com.cs.sso.service.ChangeLogService;
import com.cs.sso.service.LoginLogService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountRepository accountRepository;

    @Autowired
    private ChangeLogService changeLogService;

    @Autowired
    private LoginLogService loginLogService;

    private String key = "alexleelk"; // 密钥

    @Override
    public Account findUser(String username) throws LoginException {
        Account account = accountRepository.findByUsername(username);
        if (account == null) {
            throw new LoginException("找不到该用户");
        }
        return account;
    }

    @Override
    public ReturnVo create(Account account) throws LoginException {
        ReturnVo returnVo = new ReturnVo();
        if (StringUtils.isBlank(account.getUsername())) {
            return returnVo.failure("用户名不能空白");
        }
        if (StringUtils.isBlank(account.getName())) {
            return returnVo.failure("姓名不能空白");
        }
        if (StringUtils.isBlank(account.getPassword())) {
            return returnVo.failure("密码不能空白");
        }
        account.setStatus("OK");
        // 检查用户名不能重复
        Account check = accountRepository.findByUsername(account.getUsername());
        if (check != null) {
            throw new LoginException("用户名重复");
        }

        // 对密码进行加密
        account.setPassword(DESEncryptDecrypt.encode(account.getPassword(), key));

        // 保存
        accountRepository.save(account);

        // 记录变更记录
        ChangeLog changeLog = formatLog(account);
        changeLogService.log(changeLog);
        return returnVo.success();
    }

    @Override
    public void lockUser(String username) {
        Account account = accountRepository.findByUsername(username);
        account.setStatus("LOCK");
        accountRepository.save(account);
        // 记录变更记录
        ChangeLog changeLog = formatLog(account);
        changeLogService.log(changeLog);
    }

    @Override
    public void remove(String username) {
        Account account = accountRepository.findByUsername(username);
        account.setStatus("DENY");
        accountRepository.save(account);
        // 记录变更记录
        ChangeLog changeLog = formatLog(account);
        changeLogService.log(changeLog);
    }

    @Override
    @Transactional
    public String resetPassword(String username) {
        Account account = accountRepository.findByUsername(username);
        // 生成随机的密码
        String password = UUIDGenerator.getUUID().substring(0, 10);
        account.setPassword(AESEncryptDecrypt.encode(password, key));
        account.setStatus("OK");
        accountRepository.save(account);
        // 重置密码错误次数
        loginLogService.writeResetLog(username);
        // 记录变更记录
        ChangeLog changeLog = formatLog(account);
        changeLogService.log(changeLog);
        return password;
    }

    private ChangeLog formatLog(Account account) {
        ChangeLog changeLog = new ChangeLog();
        changeLog.setUsername(account.getUsername());
        changeLog.setName(account.getName());
        changeLog.setPassword(account.getPassword());
        changeLog.setStatus(account.getStatus());
        changeLog.setChangeTime(new Date());
        return changeLog;
    }
}
